package cn.ycxy.lbl.posts.service.impl;

import cn.ycxy.lbl.common.exception.BusinessException;
import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import cn.ycxy.lbl.pojo.posts.domain.LblCsComment;
import cn.ycxy.lbl.pojo.posts.domain.LblCsDiscussPost;
import cn.ycxy.lbl.pojo.posts.dto.CommentAddDTO;
import cn.ycxy.lbl.posts.dao.CommentDao;
import cn.ycxy.lbl.posts.dao.DiscussPostsDao;
import cn.ycxy.lbl.posts.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(rollbackFor = Exception.class)
@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, LblCsComment> implements ICommentService {

    @Autowired
    private DiscussPostsDao discussPostsDao;

    @Override
    public void addComment(CommentAddDTO commentAddDTO){
        LblCsComment lblCsComment = new LblCsComment();
        BeanUtils.copyProperties(commentAddDTO, lblCsComment);
        boolean b = save(lblCsComment);
        int i = discussPostsDao.updateCommentCountById(commentAddDTO.getPostsId());
        if (!b || i == 0) {
            throw new BusinessException(ResponseEnum.COMMENT_SAVE_FAILED);
        }
    }
}
